package com.dayuanit.dy16.atm.holder;

import java.util.List;

public class PageHolder<T> {

    private int totalPage;
    private List<T> data;
    private int pageSize = 3;
    private int currentPage = 1;
    private int totalData;

    public int getTotalData() {
        return totalData;
    }

    private PageHolder(int totalData, int currentPage) {
        this.currentPage = currentPage;
        this.totalData = totalData;
        processTotalPage(totalData);
    }

    private PageHolder(int totalData, int currentPage, int pageSize) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        processTotalPage(totalData);
    }

    public static PageHolder build(int totalData, int currentPage) {
        return new PageHolder(totalData, currentPage);
    }

    public static PageHolder build(int totalData, int currentPage, int pageSize) {
        return new PageHolder(totalData, currentPage, pageSize);
    }

    private void processTotalPage(int totalData) {
        totalPage = totalData % pageSize == 0 ? totalData / pageSize : totalData / pageSize + 1;
    }

    public int offset() {
        return (currentPage - 1) * pageSize;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public List<T> getData() {
        return data;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setData(List<T> data) {
        this.data = data;
    }
}
